package rolesystem.agentlogic.managers;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;

import rolesystem.agentlogic.utilityclasses.AggregateData;

public class DataOutputManager 
{
	static boolean console;
	
	static boolean screen;
	
	static boolean file;
	
	static long turn_counter;
	
	static public AggregateData dataSim;
	
	//static FileWriter dataFile;
	
	static FileWriter aggregateDataFile;
	
	static	
	{
		DataOutputManager.console=true;
		
		DataOutputManager.screen=true;
		
		DataOutputManager.file=false;
		
		DataOutputManager.turn_counter=0;
		
		dataSim= new AggregateData();
		
  	  	String workingDir = System.getProperty("user.dir");
	
		try {
			//DataOutputManager.dataFile= new FileWriter (workingDir + File.separator + "test.txt");
			DataOutputManager.aggregateDataFile= new FileWriter (workingDir + File.separator + "aggregatedata.txt");
			} catch (IOException e) {
				aggregateDataFile=null;
				e.printStackTrace();	
			
			
		}
	
	}
	
	public static void setOutputMode(boolean console, boolean screen, boolean file) 
	{
		DataOutputManager.console=console;
		DataOutputManager.screen=screen;
		DataOutputManager.file=file;
		  
	}
	
	public static void printString(String text) 
	{
		if(DataOutputManager.console==true)
			System.out.println(text);
		if(DataOutputManager.screen==true)
		{
			EnvironmentManager.stateVisualization.append(text);
			// Inserire gestione stile testo
		}
		if(DataOutputManager.file==true)
		{
			
		}	// Implementare
		
		  
	}
	
	/*public synchronized static void dataToFile(String text) 
	{
		try {
			dataFile.append(text);
		} catch (IOException e) {
			e.printStackTrace();
		}
		  
	}*/
	
	public static String timestamp() 
	{
		Calendar calendar = Calendar.getInstance();
		Date now = calendar.getTime();
		
		//return now.toString(); // Formato Stringa
		return String.valueOf(now.getTime());  
	}
	
	public static long timestampLong() 
	{
		Calendar calendar = Calendar.getInstance();
		Date now = calendar.getTime();
		
		//return now.toString(); // Formato Stringa
		return now.getTime();  
	}
	
	public static long getSimulationStart() 
	{
		return dataSim.getStartTime();
	}
	
	public static void SimulationStart() 
	{
		dataSim.setstartTime(DataOutputManager.timestampLong());
	}
	
	public static void SimulationStop() 
	{
		dataSim.setendTime(DataOutputManager.timestampLong());
	}
	
	/*public static void closeFile() 
	{
		try {
			dataFile.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}*/
	
	public synchronized static void aggregateDataToFile() 
	{
		try {
			aggregateDataFile.append(dataSim.toString());
			//aggregateDataFile.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		  
	}

	public synchronized static void periodicAggregateDataToFile() 
	{
		try {
			aggregateDataFile.append("Turn" + AggregateData.data_separator + turn_counter + AggregateData.row_separator + dataSim.toStringPeriodic());
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		turn_counter++;
		  
	}
	
	public synchronized static void endsimulation() 
	{
		try {
			aggregateDataFile.append(AggregateData.line_separator+"ENDSIMULATION" +  AggregateData.row_separator+ AggregateData.line_separator + AggregateData.line_separator+AggregateData.line_separator);
			aggregateDataFile.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		turn_counter=0;
		  
	}
	
	public synchronized static void closeAggregateDataFile() 
	{
		try {
			aggregateDataFile.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
		

	
	
	
}
